﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;

public partial class admin_qlconsultingdetail : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.CompareTo("addlang") == 0)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = GridView1.Rows[index];
            Label id = (Label)row.FindControl("id");
            Response.Redirect("Default.aspx?page=qlconsultingdetaillang&id=" + id.Text + "&idc="+Request.Params["id"]);
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        e.Cancel = true;
        Label id = (Label)this.GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("id");
        Label cid = (Label)this.GridView1.Rows[e.NewEditIndex].Cells[4].FindControl("cid");
        //Response.Write(id.Text);
        Response.Redirect("Default.aspx?page=edconsultingdetail&id=" + id.Text + "&cid="+cid.Text);
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        e.Cancel = true;
        Label id = (Label)this.GridView1.Rows[e.RowIndex].Cells[0].FindControl("id");
        connection con = new connection();
        SqlConnection cn = con.connect();
        //delete image
        SqlCommand mySqlSelect1 = new SqlCommand("select * from consultingdetails where id = @id", cn);
        mySqlSelect1.CommandType = CommandType.Text;
        SqlParameter paramUserName = new SqlParameter("@id", SqlDbType.BigInt);
        paramUserName.Value = Int64.Parse(id.Text);
        mySqlSelect1.Parameters.Add(paramUserName);
        try
        {
            SqlDataReader rd = mySqlSelect1.ExecuteReader();
            if (rd.HasRows)
            {
                rd.Read();
                String s = rd.GetSqlString(1).ToString();
                //info.Text = getCat((Int64)rd.GetSqlInt64(3).ToSqlInt32()).ToString();
                String path = "~" + s;
                DeleteImage(path);
            }
            rd.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            cn.Close();
            Console.WriteLine("Loi setValueFromDatabaseToForm : " + ex);
        }
        //xoa ben bang consulting_lang
        SqlCommand mySqlSelect = new SqlCommand("delete from consultingdetails_lang where id = @id", cn);
        mySqlSelect.CommandType = CommandType.Text;

        SqlParameter idt = new SqlParameter("@id", SqlDbType.BigInt);
        idt.Value = Int64.Parse(id.Text);

        mySqlSelect.Parameters.Add(idt);
        mySqlSelect.ExecuteNonQuery();
        //xoa ben bang consultingdetails_lang
        SqlCommand mySqlSelect3 = new SqlCommand("delete from consultingDetails where id = @idtt", cn);
        mySqlSelect3.CommandType = CommandType.Text;

        SqlParameter id3 = new SqlParameter("@idtt", SqlDbType.BigInt);
        id3.Value = Int64.Parse(id.Text);

        mySqlSelect3.Parameters.Add(id3);
        mySqlSelect3.ExecuteNonQuery();
        
        cn.Close();
        SqlDataSource1.DataBind();
        GridView1.DataBind();
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#99ffcc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?page=qlconsulting");
    }


    private void DeleteImage(String imageName)
    {
        String path = this.Server.MapPath(imageName);
        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script language='javascript'>alert('" + ex + "');</script>");
        }
    }

    protected void addbt_Click(object sender, EventArgs e)
    {
        info.Text = "";
        try
        {
            connection con = new connection();
            SqlConnection cn = con.connect();
            SqlCommand mySqlSelect = new SqlCommand("insert into consultingdetails values(@id,@img,@name,@add,@phone,@email,@cid)", cn);
            mySqlSelect.CommandType = CommandType.Text;

            SqlParameter na = new SqlParameter("@name", SqlDbType.NVarChar, 50);
            na.Value = name.Text;

            SqlParameter add = new SqlParameter("@add", SqlDbType.NVarChar, 150);
            add.Value = address.Text;

            SqlParameter ph = new SqlParameter("@phone", SqlDbType.NVarChar, 50);
            ph.Value = phone.Text;

            SqlParameter em = new SqlParameter("@email", SqlDbType.NVarChar, 50);
            em.Value = email.Text;

            SqlParameter id = new SqlParameter("@id", SqlDbType.BigInt);
            Double idt = Common.ToSeconds(DateTime.Now);
            id.Value = idt;

            SqlParameter idc = new SqlParameter("@cid", SqlDbType.BigInt);
            idc.Value = Request.QueryString["id"];

            SqlParameter url = new SqlParameter("@img", SqlDbType.NVarChar, 150);
            String regex = "";
            String picPath = "";
            if (img.PostedFile.ContentLength > 2097151)
            {
                info.Visible = true;
                info.Text = "Tập Tin Hình Ảnh Có Dung Lượng Nhỏ Hơn 2MB!";
                return;
            }
            else
            {
                regex = System.IO.Path.GetExtension(img.PostedFile.FileName.ToLower().ToString());
                picPath = "/image/" + idt + regex;
                if (regex == ".bmp" || regex == ".jpg" || regex == ".gif" || regex == ".png")
                {
                    Common.resizeImage(Server.MapPath("~" + picPath), 120, 80, img);
                }
                else
                {
                    info.Visible = true;
                    info.Text = "Tập Tin Hình Ảnh Có Phần Mở Rộng là .jpg,.bmp,.gif,.png!";
                }
            }
            if (regex == "")
            {
                url.Value = null;
            }
            else
            {
                url.Value = idt+regex;
            }

            mySqlSelect.Parameters.Add(na);
            mySqlSelect.Parameters.Add(add);
            mySqlSelect.Parameters.Add(ph);
            mySqlSelect.Parameters.Add(em);
            mySqlSelect.Parameters.Add(id);
            mySqlSelect.Parameters.Add(idc);
            mySqlSelect.Parameters.Add(url);
            mySqlSelect.ExecuteNonQuery();
            cn.Close();
            SqlDataSource1.DataBind();
            GridView1.DataBind();
            info.Text = "Thêm dữ liệu thành công";
        }
        catch (Exception ex)
        {
            info.Text = "Có lỗi trong quá trình nhập liệu";
        }
    }
}